package com.polaris.live.ui.me.diamond

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.polaris.live.R
import com.polaris.live.adapter.diamond.DiamondAdapter
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.databinding.ActivityDiamondBinding
import com.polaris.live.popup.DiamondPopup
import com.polaris.live.popup.GameDetailPopup
import com.polaris.live.translation.R.string
import com.polaris.live.utils.getBooleanOrString
import com.polaris.live.viewmodel.DiamondViewModel

/**
 * DiamondActivity
 *
 * @author Created by 半仙 on 2023/10/26/026 15:44
 */
class DiamondActivity : BaseVbActivity<DiamondViewModel, ActivityDiamondBinding>() {

    private val mAdapter by lazy { DiamondAdapter() }

    //判断是展示游戏币明细还是钻石明细    true展示游戏币明细
    private val mIsGameDetail by lazy { intent.getBooleanOrString("isGameDetail", false) }

    override fun initView(savedInstanceState: Bundle?) {
        mViewModel.mIsGameDetail = mIsGameDetail

        if (mIsGameDetail) {
            mBinding.bvTitle.setCenterTitle(StringUtils.getString(string.game_coin_detail))
            mAdapter.setIsGameDetail(mIsGameDetail)
            mBinding.refreshView.setEmptyTip(
                R.drawable.salalogo,
                getString(string.adapter_load_end)
            )
        } else {
            mBinding.refreshView.setEmptyTip(
                R.drawable.salalogo,
                getString(string.No_Diamond)
            )
        }

        mViewModel.onRefresh()

        mBinding.refreshView.apply {
            recycler.layoutManager = LinearLayoutManager(context)
            recycler.itemAnimator = null
            attachedAdapter(mAdapter, mViewModel).observe(
                this@DiamondActivity,
                mViewModel.diamondLiveData
            )
        }

        initListener()
    }

    private fun initListener() {
        mBinding.bvTitle.setOnLeftClientListener {
            finish()
        }
        mBinding.bvTitle.setOnRightClientListener {
            if (mIsGameDetail) {
                XPopup.Builder(this)
                    .hasShadowBg(true)
                    .isDestroyOnDismiss(true)
                    .asCustom(GameDetailPopup(this) {
                        mViewModel.userCoinDetailSourceType = it
                        mBinding.refreshView.recycler.scrollToPosition(0)
                        mBinding.refreshView.autoRefresh()
                    })
                    .show()
            } else {
                XPopup.Builder(this)
                    .hasShadowBg(true)
                    .isDestroyOnDismiss(true)
                    .asCustom(DiamondPopup(this, mViewModel.userCoinDetailSourceType) {
                        mViewModel.userCoinDetailSourceType = it
                        mBinding.refreshView.recycler.scrollToPosition(0)
                        mBinding.refreshView.autoRefresh()
                    }).show()
            }
        }
    }
}